﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using ESF.Object.XmlRepresenter;
using ESF.General;
using ESF.Object.DataBaseGenerator;
using ESF.General.Text;

#endregion


namespace ESF.Object.SolutionGenerator.Services.Core
{
	public static class DataAccessGenerator
	{
		#region Funcion para Generar Clase
		
		internal static void GenerateGeneralClass (string pstrSetName)
		{
			TextWriter txtWriter;
			
			
			txtWriter = File.CreateText (SolGenerator.SourcesDestinationFolder + Path.DirectorySeparatorChar +
				pstrSetName + Path.DirectorySeparatorChar + "Core" + Path.DirectorySeparatorChar + 
				"DataAccesses" + Path.DirectorySeparatorChar + 
				"BaseAdo.cs");
			
			txtWriter.Write ("namespace {0}.{1}.DataAccesses", SolGenerator.Namespace, pstrSetName);
			txtWriter.Write (TextHelper.NL_CO);
			txtWriter.Write ("public static class BaseAdo");
			txtWriter.Write (TextHelper.NL_1_TAB_CO);
			txtWriter.Write ("public const string CONNECTION_STRING_NAME = \"{0}\";", SolGenerator.ConnectionStringName);
			txtWriter.Write (TextHelper.NL_1_TAB_CC);
			txtWriter.Write (TextHelper.NL_CC);
			
			txtWriter.Close ();
		}
		
		
		internal static void GenerateClass (TextWriter ptxtWriter, string pstrBaseNamespace, string pstrBaseClassName, XmlObject pobjObject)
		{
			XmlObject objRefObj;
			XmlObject objExtRefObj;
		
			#region Usings
			
			ptxtWriter.WriteLine ("#region Libraries");
			ptxtWriter.WriteLine ();
			ptxtWriter.WriteLine ("using System;");
			ptxtWriter.WriteLine ("using System.Collections.Generic;");
			ptxtWriter.WriteLine ("using System.Data;");
			ptxtWriter.WriteLine ("using System.Data.Common;");
			ptxtWriter.WriteLine ("using System.Text;");
			ptxtWriter.WriteLine ();
			ptxtWriter.WriteLine ("using ESF.General.Core.DataAccess.Db;");
			ptxtWriter.WriteLine ("using ESF.General;");
			ptxtWriter.WriteLine ();
			ptxtWriter.WriteLine ("using {0}.{1}.Entities;", pstrBaseNamespace, pobjObject.SetName);
					
			SolGenerator.AddEntitiesUsings (ref ptxtWriter, pobjObject.Members);
			SolGenerator.AddControllersUsings (ref ptxtWriter, pobjObject.Members);
			
			ptxtWriter.WriteLine ();
			ptxtWriter.WriteLine ("#endregion");
			ptxtWriter.WriteLine ();
			ptxtWriter.WriteLine ();
			
			#endregion
			
			ptxtWriter.Write ("namespace {0}.{1}.DataAccesses", pstrBaseNamespace, pobjObject.SetName);
			ptxtWriter.Write (TextHelper.NL_CO);
			ptxtWriter.Write ("public static class {0}{1}", pstrBaseClassName, pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_1_TAB_CO);
			
			#region Funciones de Lectura
			
			ptxtWriter.WriteLine ("#region Read Only Functions");
			
			foreach (XmlMember objMember in pobjObject.CustomMembers)
			{
				objRefObj = SolGenerator.ObjectSetManager.GetObjectBy (objMember);
			
				if (objRefObj.IsArray)
				{
					objExtRefObj = SolGenerator.ObjectSetManager [objRefObj.SetName][objRefObj.Type];
					
					DataAccessGenerator.GenerateSelectRefArrayByMe (ref ptxtWriter, pobjObject, objMember, objExtRefObj);
				
					DataAccessGenerator.GenerateSelectArraySelfByRef (ref ptxtWriter, pobjObject, objMember, objExtRefObj);
				}
			}
		
			DataAccessGenerator.GenerateSelect (ref ptxtWriter, pobjObject);
			DataAccessGenerator.GenerateCount (ref ptxtWriter, pobjObject);
			
			//if (pobjObject.HasEnabledMember)
			//{
			//    DataAccessGenerator.GenerateSelectEnabled (ref ptxtWriter, pobjObject);
			//}
		
			//DataAccessGenerator.GenerateSelectByID (ref ptxtWriter, pobjObject);
		
			if (pobjObject.HasNameMember)
			{
				DataAccessGenerator.GenerateSelectList (ref ptxtWriter, pobjObject);
				
				if (pobjObject.HasEnabledMember)
				{
					DataAccessGenerator.GenerateSelectEnabledList (ref ptxtWriter, pobjObject);
				}
			}
			
			ptxtWriter.Write (TextHelper.NL_2_TAB);
			ptxtWriter.WriteLine ("#endregion");
			ptxtWriter.WriteLine ();
			
			#endregion
			
			#region Funciones de Edicion
			
			ptxtWriter.Write (TextHelper.NL_2_TAB);
			ptxtWriter.WriteLine ("#region Edit Functions");
			
			foreach (XmlMember objMember in pobjObject.CustomMembers)
			{
				objRefObj = SolGenerator.ObjectSetManager.GetObjectBy (objMember);
			
				if (objRefObj.IsArray)
				{
					objExtRefObj = SolGenerator.ObjectSetManager [objRefObj.SetName][objRefObj.Type];
					
					DataAccessGenerator.GenerateDeleteArrayByRef (ref ptxtWriter, pobjObject, objMember, objExtRefObj);
					DataAccessGenerator.GenerateDeleteArrayBySelf (ref ptxtWriter, pobjObject, objMember, objExtRefObj);
					
					DataAccessGenerator.GenerateInsertArray (ref ptxtWriter, pobjObject, objMember, objExtRefObj);
				}
			}
		
			DataAccessGenerator.GenerateDelete (ref ptxtWriter, pobjObject);
			DataAccessGenerator.GenerateInsertUpdate (ref ptxtWriter, pobjObject);
			
			ptxtWriter.Write (TextHelper.NL_2_TAB);
			ptxtWriter.WriteLine ("#endregion");
			ptxtWriter.WriteLine ();
			
			#endregion
			
			ptxtWriter.Write (TextHelper.NL_1_TAB_CC);
			ptxtWriter.Write (TextHelper.NL_CC);
			ptxtWriter.Close ();
		}
		
		#endregion
		
		
		#region Funciones para Lectura
		
		private static void GenerateSelectArraySelfByRef (ref TextWriter ptxtWriter, XmlObject pobjObject, XmlMember pobjMember, XmlObject pobjExtRefObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjObject.Name),
				"SelectSelfArrayOf_" + pobjObject.Name + "_By_" + pobjMember.Name, pobjObject, 
				pobjExtRefObject.Name + " pobjRI"));
				
			ptxtWriter.WriteLine ("List<{0}> objLIs;", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("{0} objLI;", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "SelectSelfArrayOf_" + pobjObject.Alias + "_By_" + pobjMember.Alias));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("DbReader.AddParameter (ref dbcCommand, \"{0}pID{1}\", pobjRI.ID);", 
				SolGenerator.DbParamsSeparator, pobjExtRefObject.Name);
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("objLIs = new List<{0}> ();", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndWhileOpenDbReader ());
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateReadObject (TextHelper.NL_4_TAB, pobjObject, "objLI", true));
			
			ptxtWriter.Write (TextHelper.NL_5_TAB);
			ptxtWriter.Write ("objLIs.Add (objLI);");
			
			//ptxtWriter.Write (TextHelper.NL_4_TAB_CC);			
			
			ptxtWriter.Write (DataAccessGenerator.GenerateCloseDbReader (TextHelper.NL_3_TAB));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("return objLIs;");
				
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateSelectRefArrayByMe (ref TextWriter ptxtWriter, XmlObject pobjObject, XmlMember pobjMember, XmlObject pobjExtRefObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjExtRefObject.Name),
				"SelectRefArrayOf_" + pobjMember.Name + "_By_" + pobjObject.Name, pobjObject,
				pobjObject.Name + " pobjI"));
				
			ptxtWriter.WriteLine ("List<{0}> objLIs;", pobjExtRefObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("{0} objLI;", pobjExtRefObject.Name);
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "SelectRefArrayOf_" + pobjMember.Alias + "_By_" + pobjObject.Alias));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("DbReader.AddParameter (ref dbcCommand, \"{0}pID{1}\", pobjI.ID);", 
				SolGenerator.DbParamsSeparator, pobjObject.Name);
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("objLIs = new List<{0}> ();", pobjExtRefObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndWhileOpenDbReader ());
			
			ptxtWriter.Write (DataAccessGenerator.GenerateReadObject (TextHelper.NL_4_TAB, pobjExtRefObject, "objLI", true));
			
			ptxtWriter.Write (TextHelper.NL_5_TAB);
			ptxtWriter.Write ("objLIs.Add (objLI);");
			
			//ptxtWriter.Write (TextHelper.NL_4_TAB_CC);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateCloseDbReader (TextHelper.NL_3_TAB));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("return objLIs;");
				
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		//private static void GenerateSelectEnabled (ref TextWriter ptxtWriter, XmlObject pobjObject)
		//{
		//    ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjObject.Name), 
		//        "SelectEnabled", pobjObject, pobjObject.Name + " pobjI", "ref PaggingInfo pobjPagInfo", "params string [] pstrCustomFields"));
				
		//    ptxtWriter.WriteLine ("pobjI.Use_{0} = true;", pobjObject.EnabledMember.Name);
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.WriteLine ("pobjI.{0} = true;", pobjObject.EnabledMember.Name);
			
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.Write ("return BaseAdo{0}.Select (pobjI, ref pobjPagInfo, pstrCustomFields);", pobjObject.Name);
			
		//    ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		//}
		
		
		private static void GenerateSelect (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjObject.Name), 
				"Select", pobjObject, pobjObject.Name + " pobjI", "ref PaggingInfo pobjPagInfo"));
			
			ptxtWriter.WriteLine ("StringBuilder strOutputFields;");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("List<{0}> objLIs;", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("{0} objLI;", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "Select"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("strOutputFields = new StringBuilder ();");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("if (pobjI.UseOutput_ID)");
			ptxtWriter.Write (TextHelper.NL_3_TAB_CO);
			ptxtWriter.Write ("strOutputFields.Append (\"ID AS {0}{1}_ID{0},\");", SolGenerator.DbNamesSeparator, pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			
			foreach (XmlMember objMember in pobjObject.GetNoArrayMembers (SolGenerator.ObjectSetManager))
			{
				ptxtWriter.Write (TextHelper.NL_3_TAB);
				ptxtWriter.Write ("if (pobjI.UseOutput_{0})", objMember.Name);
				ptxtWriter.Write (TextHelper.NL_3_TAB_CO);
				ptxtWriter.Write ("strOutputFields.Append (\"");
				
				if (objMember.IsCustom)
				{
					ptxtWriter.Write ("ID{0} AS {1}{2}_ID{3}{1},\");", objMember.Name, SolGenerator.DbNamesSeparator, pobjObject.Alias, objMember.Alias);
				}
				else
				{
					ptxtWriter.Write ("{0} AS {1}{2}_{0}{1},\");", objMember.Name, SolGenerator.DbNamesSeparator, pobjObject.Alias);
				}
				
				ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			}
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("strOutputFields.Length --;");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("objLIs = new List<{0}> ();", pobjObject.Name);
		
			#region Parametros
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("DbReader.AddParameter (ref dbcCommand, \"{0}pPageOffSet\", pobjPagInfo.PageOffSet);", SolGenerator.DbParamsSeparator);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("DbReader.AddParameter (ref dbcCommand, \"{0}pPageRowCount\", pobjPagInfo.PageRowCount);", SolGenerator.DbParamsSeparator);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("DbReader.AddParameter (ref dbcCommand, \"{0}pCustomFields\", strOutputFields.ToString ());", SolGenerator.DbParamsSeparator);
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateAddParameters (pobjObject, "pobjI"));
			
			#endregion
			
			ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndWhileOpenDbReader ());
			
			ptxtWriter.Write (DataAccessGenerator.GenerateReadObject (TextHelper.NL_5_TAB, pobjObject, "objLI", true));
			
			//ptxtWriter.WriteLine (TextHelper.NL_4_TAB_CC);			
			
			ptxtWriter.Write (DataAccessGenerator.GenerateCloseDbReader (TextHelper.NL_3_TAB));
			
			ptxtWriter.WriteLine ();
			
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("pobjPagInfo.TotalRowCount = BaseAdo{0}.Count (pobjI);", pobjObject.Name);
			
			ptxtWriter.WriteLine ();
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("return objLIs;");
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateCount (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("ulong?", pobjObject.Name), 
				"Count", pobjObject));
			ptxtWriter.Write ("return BaseAdo{0}.Count (null);", pobjObject.Name);		
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
			
		
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("ulong?", pobjObject.Name), 
				"Count", pobjObject, pobjObject.Name + " pobjI"));
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "Select"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("DbReader.AddParameter (ref dbcCommand, \"pCustomFields\", \"COUNT(ID)\");");
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateAddParameters (pobjObject, "pobjI"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("return DbReader.GetUInt64 (\"{0}\", dbcCommand, \"Count of {1}(s)\", true);",
				SolGenerator.ConnectionStringName, pobjObject.Name);
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		//private static void GenerateSelectByID (ref TextWriter ptxtWriter, XmlObject pobjObject)
		//{
		//    ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethodWPO ("void", "SelectByID", pobjObject));
			
		//    ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "SelectByID"));
			
		//    #region Parametros
			
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.WriteLine ("#region Parameters");
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pintID"", pobjI.ID, ParameterDirection.InputOutput);",
		//        SolGenerator.DbParamsSeparator);
		//    ptxtWriter.Write (TextHelper.NL_3_TAB);
		//    ptxtWriter.WriteLine ("#endregion");
			
		//    #endregion
						
		//    ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndOpenDbReader ());
			
		//    ptxtWriter.Write (DataAccessGenerator.GenerateReadID (TextHelper.NL_3_TAB, pobjObject, "pobjI"));
						
		//    ptxtWriter.Write (DataAccessGenerator.GenerateReadMembers (TextHelper.NL_3_TAB, "pobjI", false, pobjObject.Members.ToArray ()));
			
		//    ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			
		//    ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		//}
		
		
		private static void GenerateSelectList (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjObject.Name), "SelectList", pobjObject));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("List<{0}> objLIs;", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("{0} objLI;", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "SelectList"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("objLIs = new List<{0}> ();", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndWhileOpenDbReader ());
			
			ptxtWriter.Write (DataAccessGenerator.GenerateReadObject (TextHelper.NL_5_TAB, pobjObject, "objLI", true, pobjObject.NameMember, pobjObject.AbreviatureMember));
			
			//ptxtWriter.WriteLine (TextHelper.NL_4_TAB_CC);
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateCloseDbReader (TextHelper.NL_3_TAB));
			
			ptxtWriter.WriteLine ();
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("return objLIs;");
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateSelectEnabledList (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod (string.Format ("List<{0}>", pobjObject.Name), "SelectEnabledList", pobjObject));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("List<{0}> objLIs;", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("{0} objLI;", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareDbReader ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "SelectEnabledList"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("objLIs = new List<{0}> ();", pobjObject.Name);
			
			ptxtWriter.Write (DataAccessGenerator.GenerateInitializeAndWhileOpenDbReader ());
			
			ptxtWriter.Write (DataAccessGenerator.GenerateReadObject (TextHelper.NL_4_TAB, pobjObject, "objLI", true, pobjObject.NameMember, pobjObject.AbreviatureMember));
			
			//ptxtWriter.WriteLine (TextHelper.NL_4_TAB_CC);
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateCloseDbReader (TextHelper.NL_3_TAB));
			
			ptxtWriter.WriteLine ();
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("return objLIs;");
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		#endregion
		
		
		#region Funciones para Edicion
		
		private static void GenerateDelete (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod ("void", "DeleteByID", pobjObject,
				pobjObject.Name + " pobjI"));
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "DeleteByID"));
			
			#region Parametros
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#region Parameters");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID"", pobjI.ID);",
				SolGenerator.DbParamsSeparator);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#endregion");
			
			#endregion
			
			ptxtWriter.Write (DataAccessGenerator.GenerateExecuteDbCommand ());
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateInsertUpdate (ref TextWriter ptxtWriter, XmlObject pobjObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethodWPO ("void", "InsertUpdate", pobjObject));
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "InsertUpdate"));
			
			ptxtWriter.Write (DataAccessGenerator.GenerateAddParameters (pobjObject, "pobjI"));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID"", pobjI.ID, ParameterDirection.InputOutput);",
				SolGenerator.DbParamsSeparator);
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateExecuteDbCommand ());
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("try");
			ptxtWriter.Write (TextHelper.NL_3_TAB_CO);
			
			ptxtWriter.Write ("pobjI.ID = ");
			
			if (pobjObject.IDType == IDTypes.Guid)
			{
				ptxtWriter.Write ("new Guid (Convert.ToString (");
			}
			else
			{
				ptxtWriter.Write ("Convert.To{0} (", pobjObject.IDType);
			}
			
			ptxtWriter.Write ("dbcCommand.Parameters [\"{0}pID\"].Value)", SolGenerator.DbParamsSeparator);
			
			if (pobjObject.IDType == IDTypes.Guid)
			{
				ptxtWriter.Write (")");
			}
			
			ptxtWriter.Write (";");
			
			ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("catch (DbGenericException dbeEx)");
			ptxtWriter.Write (TextHelper.NL_3_TAB_CO);
			ptxtWriter.Write ("dbeEx.ChangeMessage (\"No se genero correctamente el/la '{0}', reintente mas tarde\");", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_4_TAB);
			ptxtWriter.Write ("throw dbeEx;");
			ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.Write ("catch (Exception excEx)");
			ptxtWriter.Write (TextHelper.NL_3_TAB_CO);
			ptxtWriter.Write ("throw new DbGenericException (GenericException.SYSTEM_ERROR, dbcCommand, \"No pudo ser registrada la informacion de '{0}'\", excEx);", pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB_CC);
			
			ptxtWriter.Write (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateInsertArray (ref TextWriter ptxtWriter, XmlObject pobjObject, XmlMember pobjMember, XmlObject pobjExtRefObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod ("void", 
				"Insert_ArrayOf" + pobjMember.Name, pobjObject, 
				pobjObject.Name + " pobjA",
				pobjExtRefObject.Name + " pobjB"));
			
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, "Insert_ArrayOf" + pobjMember.Alias));
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID{1}"", pobjA.ID);",
				SolGenerator.DbParamsSeparator, pobjObject.Name);
				
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID{1}"", pobjB.ID);",
				SolGenerator.DbParamsSeparator, pobjExtRefObject.Name);
			
			ptxtWriter.WriteLine (DataAccessGenerator.GenerateExecuteDbCommand ());
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateDeleteArrayByRef (ref TextWriter ptxtWriter, XmlObject pobjObject, XmlMember pobjMember, XmlObject pobjExtRefObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod ("void", 
				"Delete_ArrayOf" + pobjMember.Name + "By" + pobjExtRefObject.Name,
				pobjObject, pobjExtRefObject.Name + " pobjI"));
				
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, 
				"Delete_ArrayOf" + pobjMember.Alias + "By" + pobjExtRefObject.Alias));
			
			#region Parametros
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#region Parameters");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID{1}"", pobjI.ID);",
				SolGenerator.DbParamsSeparator, pobjExtRefObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#endregion");
			
			#endregion
			
			ptxtWriter.Write (DataAccessGenerator.GenerateExecuteDbCommand ());
			
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		
		private static void GenerateDeleteArrayBySelf (ref TextWriter ptxtWriter, XmlObject pobjObject, XmlMember pobjMember, XmlObject pobjExtRefObject)
		{
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareMethod ("void", 
				"Delete_ArrayOf" + pobjMember.Name + "By" + pobjObject.Name,
				pobjObject, pobjObject.Name + " pobjI"));
				
			ptxtWriter.Write (DataAccessGenerator.GenerateDeclareInitDbCommand (pobjObject, 
				"Delete_ArrayOf" + pobjMember.Alias + "By" + pobjObject.Alias));
			
			#region Parametros
			
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#region Parameters");
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID{1}"", pobjI.ID);",
				SolGenerator.DbParamsSeparator, pobjObject.Name);
			ptxtWriter.Write (TextHelper.NL_3_TAB);
			ptxtWriter.WriteLine ("#endregion");
			
			#endregion
			
			ptxtWriter.Write (DataAccessGenerator.GenerateExecuteDbCommand ());
			
			
			ptxtWriter.WriteLine (TextHelper.NL_2_TAB_CC);
		}
		
		#endregion
		
		
		#region Funciones de Generacion de Codigo Comun
				
		//private static string GenerateInitializeAndOpenDbReader ()
		//{
		//    StringBuilder stbResult;
			
			
		//    stbResult = new StringBuilder ();
		
		//    stbResult.Append (TextHelper.NL_3_TAB);
		//    stbResult.AppendFormat (@"dbrReader = DbReader.GetReader (""{0}"", dbcCommand);", SolGenerator.ConnectionStringName);
		//    stbResult.AppendLine ();
		//    stbResult.Append (TextHelper.NL_3_TAB);
		//    stbResult.Append ("try");
		//    stbResult.Append (TextHelper.NL_3_TAB_CO);
		//    stbResult.Append ("if (dbrReader.HasRows && dbrReader.Read ())");
		//    stbResult.Append (TextHelper.NL_4_TAB_CO);
			
		//    return stbResult.ToString ();
		//}
		
		
		private static string GenerateInitializeAndWhileOpenDbReader ()
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
		
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.AppendLine ("dbrReader = null;");
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.Append ("try");
			stbResult.Append (TextHelper.NL_3_TAB_CO);			
			stbResult.AppendFormat (@"dbrReader = DbReader.GetReader (""{0}"", dbcCommand);", SolGenerator.ConnectionStringName);
			stbResult.AppendLine ();
			stbResult.Append (TextHelper.NL_4_TAB);
			stbResult.Append ("if (dbrReader.HasRows)");
			stbResult.Append (TextHelper.NL_4_TAB_CO);
			stbResult.Append ("while (dbrReader.Read ())");
			stbResult.Append (TextHelper.NL_5_TAB_CO);
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateDeclareDbReader ()
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
		
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.AppendLine ("int intOrdinal;");
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.AppendLine ("DbDataReader dbrReader;");
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateDeclareMethodWPO (string pstrReturnType, string pstrOperationName, XmlObject pobjObject, params string [] pstrExtraParameters)
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
	
			stbResult.Append (TextHelper.NL_2_TAB);
			stbResult.AppendFormat ("public static {0} {1} (ref {2} pobjI", pstrReturnType, pstrOperationName, pobjObject.Name);
			
			if (pstrExtraParameters != null && pstrExtraParameters.Length > 0)
			{
				foreach (string strExtraParameter in pstrExtraParameters)
				{
					stbResult.AppendFormat (", {0}", strExtraParameter);
				}
			}
			
			stbResult.Append (")");
			stbResult.Append (TextHelper.NL_2_TAB_CO);
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateDeclareMethod (string pstrReturnType, string pstrOperationName, XmlObject pobjObject, params string [] pstrExtraParameters)
		{
			int intI;
		
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
		
			stbResult.Append (TextHelper.NL_2_TAB);
			stbResult.AppendFormat ("public static {0} {1} (", pstrReturnType, pstrOperationName);
			
			if (pstrExtraParameters != null && pstrExtraParameters.Length > 0)
			{
				intI = 0;
				
				stbResult.AppendFormat ("{0}", pstrExtraParameters [intI]);
				
				for (intI = 1; intI < pstrExtraParameters.Length; intI ++)
				{
					stbResult.AppendFormat (", {0}", pstrExtraParameters [intI]);
				}
			}
			
			stbResult.Append (")");
			stbResult.Append (TextHelper.NL_2_TAB_CO);
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateDeclareInitDbCommand (XmlObject pobjObject, string pstrOperationName)
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
		
			stbResult.AppendLine ("DbCommand dbcCommand;");
			stbResult.Append (DataAccessGenerator.GenerateInitDbCommand (pobjObject, pstrOperationName));
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateInitDbCommand (XmlObject pobjObject, string pstrOperationName)
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
			
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.AppendFormat (@"dbcCommand = DbReader.CreateCommand (""{0}"", CommandType.StoredProcedure);", 
				SolGenerator.ConnectionStringName);
			stbResult.AppendLine ();
			stbResult.Append (TextHelper.NL_3_TAB);
			// TODO: Probar que solo es necesario el nombre del SP sin separador de nombres
			stbResult.AppendFormat (@"dbcCommand.CommandText = ""{0}"";", DbGenerator.GenerateSimpleSPName (pobjObject, pstrOperationName)); 
			//stbResult.AppendFormat (@"dbcCommand.CommandText = ""{0}CSP_{2}_{3}_{4}{0}"";", 
			//	SolGenerator.DbNamesSeparator, SolGenerator.DbName, pobjObject.SetName, pobjObject.Alias, pstrOperationName);
			stbResult.Append (TextHelper.NL_4_TAB);
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateExecuteDbCommand ()
		{
			return string.Format ("{0}DbWriter.Execute (\"{1}\", dbcCommand);", TextHelper.NL_3_TAB, SolGenerator.ConnectionStringName);
		}
		
		
		private static string GenerateReadObject (string pstrStringTabs, XmlObject pobjObject, string pstrInstanceVarName, bool pblnAddIncludeArray, params XmlMember [] pobjMembers)
		{
			if (pobjMembers == null || pobjMembers.Length == 0)
			{
				pobjMembers = pobjObject.Members.ToArray ();
			}
		
			return
				string.Format ("{0} = new {1} ();\n", pstrInstanceVarName, pobjObject.Name) + 
				DataAccessGenerator.GenerateReadID (pstrStringTabs, pobjObject, pstrInstanceVarName) +
				DataAccessGenerator.GenerateReadMembers (pstrStringTabs, pstrInstanceVarName, pblnAddIncludeArray, pobjMembers);
		}
		
		
		private static string GenerateReadID (string pstrStringTabs, XmlObject pobjObject, string pstrInstanceVarName)
		{	
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
		
			stbResult.Append (pstrStringTabs + "\t");
			stbResult.Append ("try");
			stbResult.Append (pstrStringTabs + "\t{");
			stbResult.Append (pstrStringTabs + "\t\t");
			stbResult.AppendFormat (@"intOrdinal = dbrReader.GetOrdinal (""{0}_ID"");", pobjObject.Alias);
			stbResult.Append (pstrStringTabs + "\t}");
			stbResult.Append (pstrStringTabs + "\t");
			stbResult.Append ("catch (Exception excEx)");
			stbResult.Append (pstrStringTabs + "\t{");
			stbResult.Append (pstrStringTabs + "\t\t");
			stbResult.AppendFormat ("throw new DbGenericException (DbGenericException.DB_INVALID_COLUMN_MAPPING, dbcCommand, \"La columna {0}_ID no hace parte de la salida\", excEx);", pobjObject.Alias);
			stbResult.AppendLine (pstrStringTabs + "\t}");
			
			stbResult.Append (pstrStringTabs + "\t");
			stbResult.AppendFormat ("{0}.ID = ", pstrInstanceVarName);
			
			switch (pobjObject.IDType)
			{
				case IDTypes.Byte: 
					stbResult.Append ("dbrReader.GetByte");
					break;
				
				case IDTypes.UInt16:
					stbResult.Append ("(ushort) dbrReader.GetInt16");
					break;
					
				case IDTypes.UInt32:
					stbResult.Append ("(uint) dbrReader.GetInt32");
					break;
					
				case IDTypes.UInt64:
					stbResult.Append ("(ulong) dbrReader.GetInt64");
					break;
				
				case IDTypes.Guid:
					stbResult.Append ("dbrReader.GetGuid");
					break;
					
				default:
					stbResult.Append ("dbrReader.GetString");
					break;
			}
			
			stbResult.AppendLine (" (intOrdinal);");
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateReadMembers (string pstrStringTabs, string pstrInstanceVarName, bool pblnAddIncludeArray, params XmlMember [] pobjMembers)
		{
			StringBuilder stbResult;
			
			XmlObject objRefObj;
		
		
			stbResult = new StringBuilder ();
		
			foreach (XmlMember objMember in pobjMembers)
			{
				if (objMember == null) { continue; }
			
				stbResult.Append ("\n" + pstrStringTabs + "\t");
			
				if (objMember.Type == ValueTypes.Custom)
				{
					objRefObj = SolGenerator.ObjectSetManager.GetObjectBy (objMember);
				}
				else { objRefObj = null; }
					
				
				if (objRefObj == null || !objRefObj.IsArray)
				{
					stbResult.Append ("try");
					stbResult.Append (pstrStringTabs + "\t{");
					stbResult.Append (pstrStringTabs + "\t\t");
					stbResult.Append (string.Format (@"intOrdinal = dbrReader.GetOrdinal (""{0}_", objMember.Object.Alias));
				}			
				
				if (objRefObj != null)
				{
					if (!objRefObj.IsArray)
					{				
						stbResult.AppendLine (string.Format (@"ID{0}"");", objMember.Alias));
						stbResult.Append (pstrStringTabs + "\t}");
						stbResult.Append (pstrStringTabs + "\t");
						stbResult.Append ("catch (Exception excEx)");
						stbResult.Append (pstrStringTabs + "\t{");
						stbResult.Append (pstrStringTabs + "\t\t");
						stbResult.AppendFormat ("throw new DbGenericException (DbGenericException.DB_INVALID_COLUMN_MAPPING, dbcCommand, \"La columna {0}_ID{1} no hace parte de la salida\", excEx);", objMember.Object.Alias, objMember.Alias);
						stbResult.AppendLine (pstrStringTabs + "\t}");
					}
				}
				else
				{
					stbResult.Append (string.Format (@"{0}"");", objMember.Name));
					stbResult.Append (pstrStringTabs + "\t}");
					stbResult.Append (pstrStringTabs + "\t");
					stbResult.Append ("catch (Exception excEx)");
					stbResult.Append (pstrStringTabs + "\t{");
					stbResult.Append (pstrStringTabs + "\t\t");
					stbResult.AppendFormat ("throw new DbGenericException (DbGenericException.DB_INVALID_COLUMN_MAPPING, dbcCommand, \"La columna {0}_{1} no hace parte de la salida\", excEx);", objMember.Object.Alias, objMember.Alias);
					stbResult.AppendLine (pstrStringTabs + "\t}");
				}
				
				stbResult.Append (pstrStringTabs + "\t");
				
				if (!objMember.Required && (objRefObj == null || objRefObj.IsArray))
				{
					stbResult.Append ("if (!dbrReader.IsDBNull (intOrdinal))");
					stbResult.Append (pstrStringTabs + "\t");
					stbResult.Append ("{");
					stbResult.Append (pstrStringTabs + "\t\t");
				}
				
				stbResult.Append (string.Format (@"{0}.{1} = ", pstrInstanceVarName, objMember.Name));
				
				switch (objMember.Type)
				{
					// TODO: Soportar desde el XmlMember otros tipos de datos, char, byte[], etc. e implementarlo aca
				
					#region Custom
					
					case ValueTypes.Custom:
						if (objRefObj.IsArray)
						{
							stbResult.AppendFormat ("BaseAdo{0}.SelectRefArrayOf_{1}_By_{0} ({2});", 
								objMember.Object.Name, objMember.Name, pstrInstanceVarName);	
							stbResult.AppendLine ();
						}
						else
						{
							stbResult.Append (string.Format ("new {0} ();", objMember.CustomType));
							stbResult.Append (pstrStringTabs + "\t");
							stbResult.Append (string.Format (@"{0}.{1}.ID = ", pstrInstanceVarName, objMember.Name));
							
							switch (objRefObj.IDType)
							{
								case IDTypes.Byte: 
									stbResult.Append ("dbrReader.GetByte");
									break;
								
								case IDTypes.UInt16:
									stbResult.Append ("(ushort) dbrReader.GetInt16");
									break;
									
								case IDTypes.UInt32:
									stbResult.Append ("(uint) dbrReader.GetInt32");
									break;
									
								case IDTypes.UInt64:
									stbResult.Append ("(ulong) dbrReader.GetInt64");
									break;
							}
						
							stbResult.Append (" (intOrdinal);");
						}
						
						break;
						
					#endregion
					
					#region Bool
						
					case ValueTypes.Bool:
						stbResult.Append ("dbrReader.GetBoolean (intOrdinal);");
						break;
					
					#endregion
					
					#region Guid
						
					case ValueTypes.Guid:
						stbResult.Append ("dbrReader.GetGuid (intOrdinal);");
						break;
					
					#endregion
					
					#region Byte
						
					case ValueTypes.Byte:
						stbResult.Append ("dbrReader.GetByte (intOrdinal);");
						break;
					
					#endregion
					
					#region SByte
						
					case ValueTypes.SByte:
						stbResult.Append ("dbrReader.GetInt16 (intOrdinal);");
						break;
					
					#endregion
					
					#region Date, DateTime
						
					case ValueTypes.Date: goto case ValueTypes.DateTime;
						
					case ValueTypes.DateTime: 
						stbResult.Append ("dbrReader.GetDateTime (intOrdinal);");
						break;
					
					#endregion
					
					#region Decimal
						
					case ValueTypes.Decimal:
						stbResult.Append ("dbrReader.GetDecimal (intOrdinal);");
						break;
					
					#endregion
					
					#region Double
						
					case ValueTypes.Double:
						stbResult.Append ("dbrReader.GetDouble (intOrdinal);");
						break;
					
					#endregion
					
					#region Float
						
					case ValueTypes.Float:
						stbResult.Append ("dbrReader.GetFloat (intOrdinal);");
						break;
					
					#endregion
					
					#region Integer, UnsignedInteger
					
					case ValueTypes.UInt32:
						stbResult.Append ("(uint) ");
						goto case ValueTypes.Int32;
					
					case ValueTypes.Int32:
						stbResult.Append ("dbrReader.GetInt32 (intOrdinal);");
						break;
					
					#endregion
					
					#region Long, UnsignedLong
						
					case ValueTypes.UInt64: 
						stbResult.Append ("(ulong) ");
						goto case ValueTypes.Int64;
						
					case ValueTypes.Int64:
						stbResult.Append ("dbrReader.GetInt64 (intOrdinal);");
						break;
					
					#endregion
					
					#region Short, UnsignedShort
						
					case ValueTypes.UInt16: 
						stbResult.Append ("(ushort) ");
						goto case ValueTypes.Int16;
						
					case ValueTypes.Int16:
						stbResult.Append ("dbrReader.GetInt16 (intOrdinal);");
						break;
					
					#endregion
					
					#region Default, String
					
					default:
						stbResult.Append ("dbrReader.GetString (intOrdinal);");
						break;
						
					#endregion
				}
				
				if (!objMember.Required && (objRefObj == null || objRefObj.IsArray))
				{
					stbResult.Append (pstrStringTabs + "\t");
					stbResult.AppendLine ("}");
				}
			}		
			
			if (pblnAddIncludeArray)
			{
				stbResult.Append (pstrStringTabs + "\t");
				stbResult.Append ("objLIs.Add (objLI);");
			}
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateCloseDbReader (string pstrStringTabs)
		{
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
			
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("catch (GenericException)");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("throw;");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("catch (Exception excEx)");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("throw new DbGenericException (excEx, dbcCommand);");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("finally");
			stbResult.Append (pstrStringTabs);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("if (dbrReader != null)");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("try");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_3);
			stbResult.Append ("dbrReader.Close ();");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("catch (Exception excEx)");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("{");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_3);
			stbResult.Append ("throw new DbGenericException (excEx, dbcCommand);");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_2);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs + TextHelper.TAB_1);
			stbResult.Append ("}");
			stbResult.Append (pstrStringTabs);
			stbResult.AppendLine ("}");
			 
			return stbResult.ToString ();
		}

		
		private static string GenerateAddParameters (XmlObject pobjObject, string pstrVarName)
		{
			StringBuilder stbResult;
			
		
			stbResult = new StringBuilder ();
		
			stbResult.Append (TextHelper.NL_3_TAB);
			stbResult.AppendFormat ("if ({0} != null)", pstrVarName);
			stbResult.Append (TextHelper.NL_3_TAB_CO);
		
			if (pobjObject.IDType != null)
			{
				stbResult.Append (TextHelper.NL_4_TAB);
				stbResult.AppendFormat ("if ({0}.UseFilter_ID && !{0}.IsNull_ID)", pstrVarName);
				stbResult.Append (TextHelper.NL_4_TAB_CO);			
				stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID"", pobjI.ID);", SolGenerator.DbParamsSeparator);
				stbResult.Append (TextHelper.NL_5_TAB);
				stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}pUseFilter_ID"", pobjI.ComparisonType_ID);", SolGenerator.DbParamsSeparator);
				stbResult.Append (TextHelper.NL_4_TAB_CC); 
				stbResult.Append (TextHelper.NL_4_TAB);
				stbResult.AppendFormat ("else");
				stbResult.Append (TextHelper.NL_4_TAB_CO);			
				stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}pID"");",	SolGenerator.DbParamsSeparator);
				stbResult.Append (TextHelper.NL_5_TAB);
				stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}pUseFilter_ID"");", SolGenerator.DbParamsSeparator);
				stbResult.AppendLine (TextHelper.NL_4_TAB_CC); 
			}
			
			foreach (XmlMember objMember in pobjObject.Members)
			{
				stbResult.AppendLine (DataAccessGenerator.GenerateAddParameter (objMember, pstrVarName));
			}
			
			stbResult.Append (TextHelper.NL_3_TAB_CC);
			
			return stbResult.ToString ();
		}
		
		
		private static string GenerateAddParameter (XmlMember pobjMember, string pstrVarName)
		{
			XmlObject objRefObj;
		
			StringBuilder stbResult;
			
			
			stbResult = new StringBuilder ();
			
			if (pobjMember.IsCustom)
			{
				objRefObj = SolGenerator.ObjectSetManager.GetObjectBy (pobjMember);
				
				if (!objRefObj.IsArray)
				{
					stbResult.Append (TextHelper.NL_4_TAB);
					stbResult.AppendFormat ("if ({0}.UseFilter_{1} && !{0}.IsNull_{1})", pstrVarName, pobjMember.Name);
					stbResult.Append (TextHelper.NL_4_TAB_CO);
					stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}pID{1}"", {2}.{1}.ID);",
						SolGenerator.DbParamsSeparator, pobjMember.Name, pstrVarName);
					stbResult.Append (TextHelper.NL_5_TAB);
					stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}pUseFilter_ID{1}"", pobjI.ComparisonType_{1});", 
						SolGenerator.DbParamsSeparator, pobjMember.Name);
					stbResult.Append (TextHelper.NL_4_TAB_CC);
					
					stbResult.Append (TextHelper.TAB_4);
					stbResult.Append ("else");
					stbResult.Append (TextHelper.NL_4_TAB_CO);
					stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}pID{1}"");",
						SolGenerator.DbParamsSeparator, pobjMember.Name);
					stbResult.Append (TextHelper.NL_5_TAB);
					stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}pUseFilter_ID{1}"");", 
						SolGenerator.DbParamsSeparator, pobjMember.Name);
					stbResult.Append (TextHelper.NL_4_TAB_CC);
				}
			}
			else
			{
			
				stbResult.Append (TextHelper.NL_4_TAB);
				stbResult.AppendFormat ("if ({0}.UseFilter_{1} && !{0}.IsNull_{1})", pstrVarName, pobjMember.Name);
				stbResult.Append (TextHelper.NL_4_TAB_CO);
				stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}p{1}"", {2}.{1}, ParameterDirection.Input, pobjI.ComparisonType_{1});",
					SolGenerator.DbParamsSeparator, pobjMember.Name, pstrVarName);
				stbResult.Append (TextHelper.NL_5_TAB);
				stbResult.AppendFormat (@"DbReader.AddParameter (ref dbcCommand, ""{0}pUseFilter_{1}"", pobjI.ComparisonType_{1});", 
					SolGenerator.DbParamsSeparator, pobjMember.Name);
				stbResult.Append (TextHelper.NL_4_TAB_CC);
				
				stbResult.Append (TextHelper.TAB_4);
				stbResult.Append ("else");
				stbResult.Append (TextHelper.NL_4_TAB_CO);
				stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}p{1}"");",
					SolGenerator.DbParamsSeparator, pobjMember.Name);
				stbResult.Append (TextHelper.NL_5_TAB);
					stbResult.AppendFormat (@"DbReader.AddNullParameter (ref dbcCommand, ""{0}pUseFilter_{1}"");", 
						SolGenerator.DbParamsSeparator, pobjMember.Name);
				stbResult.Append (TextHelper.NL_4_TAB_CC);
			}
			
			return stbResult.ToString ();
		}
		
		#endregion
	}
}
